package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cc.orange.databinding.FragmentDiscover2Binding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import cc.orange.adapter.Discover2Adapter;
import cc.orange.base.BaseFragment;
import cc.orange.entity.MsgInfosVideoEntity;
import cc.orange.http.ApiService;

import cc.orange.R;

import cc.orange.entity.IsLoginsEntity;
import cc.orange.utils.ZXToastUtil;

public class Discover2Fragment extends BaseFragment {

    private FragmentDiscover2Binding binding;
    private Discover2Adapter adapter;
    private ArrayList<MsgInfosVideoEntity.Data> list = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_discover2,container,false);
        initViews();
        returnVideo();
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHight(IsLoginsEntity entity){
        if(entity.isLogins()){
            list.clear();
            returnVideo();
        }else{
            list.clear();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        binding.strollFrame.setLayoutManager(layoutManager);

        //下拉刷新
        binding.smartRL.setRefreshHeader(new ClassicsHeader(getActivity()));
        binding.smartRL.setRefreshFooter(new ClassicsFooter(getActivity()));
        binding.smartRL.setEnableLoadMore(true);
        binding.smartRL.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                returnVideo();
                binding.smartRL.finishRefresh(3);
            }
        });
        binding.smartRL.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                returnVideo();
                binding.smartRL.finishLoadMore(3);
            }
        });
        adapter = new Discover2Adapter(list);
        binding.strollFrame.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), VideoActivity.class);
                intent.putExtra("uid",""+list.get(position).getSocVideo().getWomanId());
                intent.putExtra("id",""+list.get(position).getSocVideo().getId());
                intent.putExtra("video",list.get(position).getSocVideo().getVideo());
                intent.putExtra("img",list.get(position).getSocVideo().getImage());
                intent.putExtra("title",list.get(position).getSocVideo().getTitle());
                intent.putExtra("name",list.get(position).getWomanName());
                intent.putExtra("flag",list.get(position).getDianzan());
                intent.putExtra("zNum",list.get(position).getDzNum());
                startActivity(intent);
            }
        });

    }

    private void returnVideo(){
        RetrofitFactory.create(ApiService.class)
                .returnVideoF(getTokens())
                .enqueue(new DefaultCallback<MsgInfosVideoEntity>() {
                    @Override
                    public void onStart(Call<MsgInfosVideoEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<MsgInfosVideoEntity> call, HttpError error) {
                        cancelLoading();
                        if(list.size() > 0){
                            binding.llNone.setVisibility(View.GONE);
                        }else{
                            binding.llNone.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onSuccess(Call<MsgInfosVideoEntity> call, MsgInfosVideoEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            if(baseEntity.getData().size() > 0){
                                list.addAll(baseEntity.getData());
                            }
                            if(list.size() > 0){
                                binding.llNone.setVisibility(View.GONE);
                            }else{
                                binding.llNone.setVisibility(View.VISIBLE);
                            }
                            adapter.notifyDataSetChanged();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    @Override
    public void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}